package com.ythook.mentor.controller;

import com.ythook.mentor.entity.Trainings;
import com.ythook.mentor.entity.TrainingsDetail;
import com.ythook.mentor.service.TrainingsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping("/api")
public class TrainingsController {
    @Resource
    private TrainingsService trainingsService;
    private Logger logger = LoggerFactory.getLogger(TrainingsController.class);

    @GetMapping("/trainings")
    public ResponseEntity<List<Trainings>> search() {
        return ResponseEntity.ok(trainingsService.search());
    }

    @GetMapping("/trainings/searchAll")
    public ResponseEntity<List<TrainingsDetail>> searchAll() {
        return ResponseEntity.ok(trainingsService.searchAll());
    }

    @GetMapping("/trainings/findById/{id}")
    public ResponseEntity<Trainings> findById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(trainingsService.findById(id).orElse(new Trainings()));
    }

    @GetMapping("/trainings/findByUserId/{id}/{trainingStatus}")
    public ResponseEntity<List<TrainingsDetail>> findByUserId(@PathVariable("id") Long id, @PathVariable("trainingStatus") String trainingStatus) {
        return ResponseEntity.ok(trainingsService.findByUserId(id, trainingStatus));
    }

    @GetMapping("/trainings/findByMentorId/{id}/{trainingStatus}")
    public ResponseEntity<List<TrainingsDetail>> findByMentorId(@PathVariable("id") Long id, @PathVariable("trainingStatus") String trainingStatus) {
        return ResponseEntity.ok(trainingsService.findByMentorId(id, trainingStatus));
    }

    @GetMapping("/trainings/findByMentorId/{id}")
    public ResponseEntity<List<Trainings>> findByMentorId(@PathVariable("id") Long id) {
        return ResponseEntity.ok(trainingsService.findByMentorId(id));
    }
    @DeleteMapping("/trainings/delete/{id}")
    public ResponseEntity<Object> delete(@PathVariable("id") Long id){
        trainingsService.delete(id);
        Map<String,Object> map = new HashMap<>();
        map.put("status","success");
        return ResponseEntity.ok(map);
    }

    @PostMapping("/trainings/add")
    public ResponseEntity<HttpStatus> add(@RequestBody Trainings trainings){
        trainingsService.add(trainings);
        return ResponseEntity.ok(HttpStatus.CREATED);
    }

    @PutMapping("/trainings/update")
    public ResponseEntity<Object> update(@RequestBody Trainings trainings){
        Map<String,Object> map = new HashMap<>();
        map.put("status","success");
        map.put("data", trainingsService.update(trainings));
        return ResponseEntity.ok(map);
    }
}
